package com.firewof.redis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * Jedis连接池工具类
 * @author liuxing
 *
 */
public class JedisPoolUtil {
	//Jedis连接池
	private static volatile JedisPool jedisPool;
	
	private JedisPoolUtil(){};
	
	/**
	 * 获取连接池
	 * @return
	 */
	public static JedisPool getJedisPool(){
		if(null == jedisPool){
			synchronized(JedisPoolUtil.class){
				 if (null == jedisPool) { 
					 JedisPoolConfig poolConfig = new JedisPoolConfig(); 
					 poolConfig.setMaxTotal(1000); //poolConfig.setMaxActive(1000);
					 poolConfig.setMaxIdle(32);
					 poolConfig.setMaxWaitMillis(100*1000); //poolConfig.setMaxWait(100*1000);
					 poolConfig.setTestOnBorrow( true );
					 jedisPool = new JedisPool(poolConfig, "127.0.0.1",6379); 
				  } 
			}
		}
		return jedisPool;
	}
	
	/**
	 * 释放连接
	 * @param jedis
	 */
	public static void release(Jedis jedis){
		jedisPool.returnBrokenResource(jedis);
	}
}
